<?php
namespace Admin\Model;
use Think\Model;
class FinanceModel extends Model{
	public $autoCheckFields = false;
	public $pay_type = '';

	/**
	 * 提现到银行卡
	 * @param  [type] $take_out_id 提现ID
	 */
	public function take_out_to_bank($take_out_id){
		$where['id'] = $take_out_id;
		$info = D('UserTakeOut')->where($where)->find();
		if($info['status'] != '1'){
			$result['info']   = '非审核状态，无法提现！';
			$result['status'] = '400';
		}
		if($info['status'] == '4' || $info['trade_no']){
			$result['info']   = '已提现成功！';
			$result['status'] = '400';
		}

		unset($where);
		$where['card_id'] = $info['card_id'];
		$bank_carid = D('UserBankCard')->where($where)->find();
		$bank_carid['bank_name'] = D('Category')->get_name($bank_carid['card_type']);
		$bank_carid['city_name'] = D('Area')->id2names($bank_carid['city_id']);

		import("KuaiQianPayService", 'Public/Class/Pay/KuaiQian');
		$pay = new KuaiQianPayService();
		$order_sn       = $info['create_time'] . '_' . $take_out_id;
		$realname       = $bank_carid['account_name'];
		$city_name      = $bank_carid['city_name'];
		$bank_name      = $bank_carid['bank_name'];
		$open_bank_name = $bank_carid['city_name'] . '支行';
		$bank_card_no   = $bank_carid['card_no'];
		$amount         = $info['money'];
		$result = $pay->pay_to_bank($order_sn, $realname, $city_name, $bank_name, $open_bank_name, $bank_card_no, $amount);
		if($result['status'] == '200'){
			$data['trade_no']    = $result['data']['dealId'];
			$data['deal_charge'] = $result['data']['dealCharge'];
			$data['status']      = '4';//提现中
			$pay_status          = '';

			$result['data']['status_desc']        = '受理中';
			$result['data']['platform_bank_name'] = '快钱支付';
			$result['data']['update_time_desc']   = date('Y-m-d H:i:s');
		}else{
			$data['status'] = '1';
			$pay_status     = $result['info'];
		}

		$admin_id = session('user_id');
		$platform_bank_id = C('kuaiqian_bank_id');

		$data['admin_id']         = $admin_id;
		$data['update_time']      = time();
		$data['platform_bank_id'] = $platform_bank_id ? $platform_bank_id : 0;
		$data['pay_status']       = $pay_status;

		unset($where);
		$where['id'] = $take_out_id;
		D('UserTakeOut')->where($where)->save($data);
		return $result;
	}
	/**
	 * 查询提现是否到账
	 * @param  [type] $take_out_id [description]
	 * @return [type]              [description]
	 */
	public function take_out_status_query($take_out_id){
		$where['id'] = $take_out_id;
		$info = D('UserTakeOut')->where($where)->find();
		if($info['trade_no'] && $info['platform_bank_id'] != C('kuaiqian_bank_id')){
			$result['status'] = '400';
			$result['data']   = null;
			$result['info']   = '此记录非快钱提现单，无法处理！';
			return $result;
		}
		// $order_sn = $info['create_time'] . '_' . $take_out_id;

		import("KuaiQianPayService", 'Public/Class/Pay/KuaiQian');
		$pay = new KuaiQianPayService();

		$result = $pay->query_pay_bank_status($info['trade_no']);

		$deal_status = $result['data']['dealStatus'];
		// $deal_status = '114';
		// $result['status'] = '200';
		//提现成功
		if($deal_status == '111' && $info['status'] != 2){
			$data['status'] = 2;
			unset($where);
			$where['id'] = $take_out_id;
			D('UserTakeOut')->where($where)->save($data);
			//提现成功消息推送
			D('Message')->bill_msg_push($take_out_id, 'take_out_success');
		}

		//提现失败
		if($deal_status == '114' && $info['status'] != 3){
			$remark = '银行打款受理失败';

			$data['status'] = 3;
			$data['remark'] = $remark;

			unset($where);
			$where['id'] = $take_out_id;
			D('UserTakeOut')->where($where)->save($data);

			//退款处理
			D('Finance')->take_out_refund($info['user_id'], $info['money'], $info['user_type'], $remark);
			//提现成功消息推送
			D('Message')->bill_msg_push($take_out_id, 'take_out_error');
		}

		return $result;
	}
	
	/**
	 * 过滤账单字段
	 * @param  string $data 单条记录
	 */
	public function filter_field($data){
		//账户类型
		$data['account_type_name'] = get_type_desc($data['account_type'], 'account_type_desc');
		//获取用户名称
		$data['realname']          = D('User')->get_user_name($data['user_id']);
		//账单类型
		$data['bill_type_name']    = get_type_desc($data['bill_type'], 'bill_type_desc');
		//价格描述
		$data['money_desc']        = ($data['money'] > 0 ? '+' : '').$data['money'];
		//价格颜色
		$data['money_color']       = $data['money'] > 0 ? '#ff4c26' : '#6aaf66';
		//账单时间
		$data['datetime']          = date('Y-m-d H:i:s', $data['create_time']);
		//交易方式
		$data['pay_mode']          = get_type_desc($data['pay_type'], 'pay_type_desc');
		//终端类型
		$data['app_type_name']     = get_type_desc($data['app_type'], 'app_type_desc');
		$data['content']           = $data['remark'];

		return $data;
	}
	public function take_out_filter($data){
		$data['realname']           = D('User')->get_user_name($data['user_id']);
		$data['admin_name']         = D('User')->get_admin_name($data['admin_id']);
		$data['status_desc']        = get_type_desc($data['status'], 'take_out_status_desc');
		$data['datetime']           = date('Y-m-d H:i:s',$data['create_time']);
		$data['update_time_desc']   = $data['update_time'] ? date('Y-m-d H:i:s',$data['update_time']) : '';
		
		$data['app_type_name']      = get_type_desc($data['app_type'], 'app_type_desc');
		//平台提现银行
		$data['platform_bank_name'] = D('Category')->get_name($data['platform_bank_id']);
		return $data;
	}
}




